package vn.com.demand.widget;

import java.sql.SQLException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import vn.com.demand.lux.Activator;

import com.swtdesigner.ResourceManager;

public class SelectItemCodeHeader extends TitleAreaDialog {

	private static final int REFRESH_ID = 1986;
	private Text itemCode;
	private SearcheableTable searcheableTable;
	int tableStyle = SWT.BORDER | SWT.FULL_SELECTION;

	private String[] selectedItemsCode;
	private String sql;
	private ItemBrowserCommand browserCommand;
	private String dialogTitle;
	private String areaTitle;
	private String areaMessage;
	private String descriptionLabelColumn;
	private String codeLabelColumn;
	private Object selectedObject;

	public Object getSelectedObject() {
		return selectedObject;
	}

	/**
	 * Create the dialog
	 * 
	 * @param parentShell
	 * @param monthlyPlanEdtior
	 * @param costCenter
	 */
	public SelectItemCodeHeader(Shell parentShell, String sql) {
		this(parentShell, new ItemBrowserCommand(sql, null, null));
	}

	public SelectItemCodeHeader(Shell parentShell, ItemBrowserCommand command) {
		this(parentShell, command, "Browser", "Browser", "Browse and select a row then click ok");
	}
	
	public SelectItemCodeHeader(Shell parentShell, ItemBrowserCommand command, String labelColumn) {
		this(parentShell, command, "Browser", "Browser", "Browse and select a row then click ok", labelColumn);
	}
	
	public SelectItemCodeHeader(Shell parentShell, ItemBrowserCommand command, String dialogTitle, String areaTitle, String areaMessage) {
		super(parentShell);
		this.browserCommand = command;
		this.dialogTitle = dialogTitle;
		this.areaTitle = areaTitle;
		this.areaMessage = areaMessage;
	}
	
	public SelectItemCodeHeader(Shell parentShell, ItemBrowserCommand command, String dialogTitle, String areaTitle, String areaMessage, String labelColumn) {
		this(parentShell, command, dialogTitle, areaTitle, areaMessage);
		this.descriptionLabelColumn = labelColumn;
	}
	
	/**
	 * Create contents of the dialog
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayout(new FormLayout());
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

		itemCode = new Text(container, SWT.BORDER);
		final FormData fd_itemCode = new FormData();
		fd_itemCode.bottom = new FormAttachment(0, 42);
		fd_itemCode.right = new FormAttachment(0, 250);
		fd_itemCode.left = new FormAttachment(0, 5);
		itemCode.setLayoutData(fd_itemCode);
		try {
				searcheableTable = new SearcheableTable(container, getTableStyle(),
						browserCommand, null);
				if (null != descriptionLabelColumn) {
					this.setDescriptionColumnLabel(descriptionLabelColumn);
				}
				if (null != codeLabelColumn) {
					this.setCodeColumnLabel(codeLabelColumn);
				}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		final FormData fd_searcheableTable = new FormData();
		fd_searcheableTable.top = new FormAttachment(0, 47);
		fd_searcheableTable.left = new FormAttachment(0, 5);
		fd_searcheableTable.right = new FormAttachment(100, -5);
		fd_searcheableTable.bottom = new FormAttachment(100, -5);
		searcheableTable.setLayoutData(fd_searcheableTable);
		
		makeAction();
		
		itemCode.setFocus();

		Label inputLabel;
		inputLabel = new Label(container, SWT.NONE);
		fd_itemCode.top = new FormAttachment(inputLabel, 5, SWT.BOTTOM);
		final FormData fd_inputLabel = new FormData();
		fd_inputLabel.right = new FormAttachment(itemCode, 0, SWT.RIGHT);
		fd_inputLabel.top = new FormAttachment(0, 5);
		fd_inputLabel.left = new FormAttachment(itemCode, 0, SWT.LEFT);
		inputLabel.setLayoutData(fd_inputLabel);
		inputLabel.setText("Input:");
		setMessage(areaMessage);
		setTitle(areaTitle);
		//
		return area;
	}

	private void makeAction() {
		itemCode.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent event) {
				searcheableTable.setFilterValue(itemCode.getText());
			}
			
		});
	}
	
	private void setDescriptionColumnLabel(String columnName) {
		if (null != searcheableTable) {
			searcheableTable.getDescriptionColumn().setText(columnName);
		}
	}
	
	private void setCodeColumnLabel(String label) {
		if (null != searcheableTable) {
			searcheableTable.getCodeColumn().setText(label);
		}
	}
	
	/**
	 * Create contents of the button bar
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		final Button button = createButton(parent, OK, "&Ok", true);
		button.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/actions/ok.png"));
		final Button button_1 = createButton(parent, REFRESH_ID, "&Refresh", true);
		button_1.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/actions/reload.png"));
		final Button button_2 = createButton(parent, IDialogConstants.CANCEL_ID, "&Close", false);
		button_2.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/actions/shutdown.png"));
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(786, 493);
	}

	@Override
	protected void buttonPressed(int buttonId) {
		switch (buttonId) {
		case Dialog.OK:
			selectedItemsCode = searcheableTable.getSelectedItemsCode();
			selectedObject = searcheableTable.getSelectedITem();
			super.buttonPressed(buttonId);
			return;
		case REFRESH_ID:
			return;
		case Dialog.CANCEL:
			super.buttonPressed(buttonId);
		default:
			break;
		}
	}
	
	public String getSelectedItem() {
		String[] selectedItems = getSelectedItems();
		return selectedItems != null && selectedItems.length > 0 ? selectedItems[0] : null;
	}
	
	public String[] getSelectedItems() {
		return selectedItemsCode;
	}

	public void setItemBrowserCommand(ItemBrowserCommand browserCommand) {
		this.browserCommand = browserCommand;
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(dialogTitle);
	}

	public SearcheableTable getSearcheableTable() {
		return searcheableTable;
	}

	public void setSearcheableTable(SearcheableTable searcheableTable) {
		this.searcheableTable = searcheableTable;
	}

	public String getDescriptionLabel() {
		return descriptionLabelColumn;
	}

	public void setDescriptionLabel(String labelColumn) {
		this.descriptionLabelColumn = labelColumn;
	}

	public int getTableStyle() {
		return tableStyle;
	}

	public void setTableStyle(int tableStyle) {
		this.tableStyle = this.tableStyle | tableStyle;
	}

	public String getCodeLabel() {
		return codeLabelColumn;
	}

	public void setCodeLabel(String codeLabelColumn) {
		this.codeLabelColumn = codeLabelColumn;
	}
}